<?php
	include_once 'isLogin.php';
	$infoc=new info();
	$table_info = 'info';  	  		 	  //咨询表
	$table_info_label = 'info_label';  	  //咨询标签关联表表
	$id=$_GET['id'];

	//修改
	if($id!='' && $id!=null){
		$row=$infoc->info($id);

		$select_label=$commonc->getList($table_info_label." WHERE info_id=".$row['id']." ORDER BY label_id");
		$select_label=$select_label['result'];
		if (is_array($select_label)) {
			foreach ($select_label as $k1 => $v1) {
				$label_id.=$v1['label_id'].",";	
			}
		}
		//存入隐藏框
		$smarty->assign('label_id',$label_id);
		//修改读取用
		$label_id=substr($label_id,0,-1);
		$row['label_id']=explode(',',$label_id);

		$smarty->assign('row',$row);
	}

	if($_POST['submit']){
		//上传图片，生成缩略图
		$thumbnail=$row['thumbnail'];
		if($_FILES['thumbnail']['name']!=''){
			//设置路径
			$folder = "../upload/info/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";

			//删除旧图
			@unlink($annexFolder.$old_img);
			@unlink($smallFolder.$old_img);
		
			//上传新图
			$upimg = new uploadImg();
			$thumbnail = $upimg->upLoad('thumbnail',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$thumbnail,$smallFolder);
		}

		$label_id=$_POST['label_id'];
		
		$arr= array('title' =>$_POST['title'],
					'intro' =>$_POST['intro'],
					'thumbnail' =>$thumbnail,
					'class_id' =>$_POST['class_id'],	
			 );


		//批量数组
		if($label_id!='' && $label_id!=null){
			$label_id=substr($label_id,0,-1);
			$label_id=explode(',',$label_id);
			if (is_array($label_id)) {
				foreach ($label_id as $k => $v) {
					$list[$k]=array('label_id'=>$v);
				}
			}
		}


		if($id!='' && $id!=null){//update
			$check=$_POST['check'];
			if($check!='' && $check!=null){
				$arr['check']=$check;
			}

			$res=$infoc->info_update($id,$arr,$list);	
		}else{//insert
			$arr['type']='admin';
			$arr['user_id']=$_COOKIE['admin_id'];
			$arr['create_time']=time();
			$arr['check']='pass';

			$res=$infoc->info_add($arr,$list);	
		}
		
		if($res){
			$msg="操作成功";
			$href="info_list.php";
		}else{
			$msg="操作失败";
			$href="info_list.php";
		}
		echo $commonc->submitAlert($msg,$href);
		exit;
	}

	//读取分类
	$info_class=$infoc->info_class_list();
	$smarty->assign('info_class',$info_class['result']);

	//读取标签
	$label=$infoc->label_list();
	$smarty->assign('label',$label['result']);
	
	$smarty->display("info_add.html");
?>